package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.SetmealDTO;
import com.itheima.service.CheckgroupService;
import com.itheima.service.CheckitemService;
import com.itheima.service.SetmealGroupService;
import com.itheima.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.support.PageResult;
import com.itheima.support.QueryPageBean;
import com.itheima.support.SysConsts;
import com.itheima.utils.OSSUtils;
import com.itheima.utils.RedisUtil;
import com.itheima.vo.CheckGroupVo;
import com.itheima.vo.SetmealVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@DubboService
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealGroupService setmealGroupService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private CheckgroupService checkgroupService;
    @Autowired
    private CheckitemService checkitemService;
    @Override
    public SetmealVo findSetMailDetail(Long sId) {

        if (RedisUtil.get("setmeal:"+sId)!=null ){
                return RedisUtil.get("setmeal:"+sId);
        }
        SetmealVo setmealVo= baseMapper.findSetmealVoBySid(sId);
        setmealVo.setCheckGroups(checkgroupService.findGroupsBysId(sId));
        for (CheckGroupVo checkGroupVo : setmealVo.getCheckGroups()) {
            checkGroupVo.setCheckItems(checkitemService.findItemsBygId(checkGroupVo.getId()));
        }
        RedisUtil.set("setmeal:"+sId,setmealVo);
        return setmealVo;
    }

    @Override
    public List<Setmeal> findAll() {
            if(RedisUtil.get("setmeal:all") !=null){
                return RedisUtil.get("setmeal:all");
            }
            List<Setmeal> list = this.list();
            RedisUtil.set("setmeal:all",list);
            return list;
    }

    @Override
    public void clear() {
        Set<Object> members = redisTemplate.opsForSet().members("file:templateRubbish");
        if (ObjectUtils.isEmpty(members)==false){
            for (Object member : members) {
                String path= "file:"+(String)member;
                if (redisTemplate.hasKey(path)==false){
                    OSSUtils.delete((String) member);
                }
            }
        }

    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        String queryString=queryPageBean.getQueryString();
        if (ObjectUtils.isEmpty(queryString)==false){
            queryWrapper.and(wrapper->{
                wrapper.like("code",queryString).or()
                        .like("name",queryString).or()
                        .like("help_code",queryString);
            });
        }
        Page<Setmeal> page = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    @Transactional
    public Boolean saveSetmeal(SetmealDTO setmealDTO) {
        redisTemplate.delete("setmeal:"+setmealDTO.getSetmeal().getId());
        redisTemplate.delete("setmeal:all");
        saveOrUpdate(setmealDTO.getSetmeal());

        if (ObjectUtils.isEmpty(setmealDTO.getSetmeal().getId())==false){
            redisTemplate.opsForSet().remove("file:templateRubbish",setmealDTO.getSetmeal().getImg());
        }

        setmealGroupService.addByCheckGroupIds(setmealDTO.getCheckGroupIds(),setmealDTO.getSetmeal().getId());
        return true;
    }
}